package ru.incoremedia.gwttest.application.client.view.user.noteitem;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import ru.incoremedia.gwttest.application.client.i18n.I18N;
import ru.incoremedia.gwttest.application.client.view.user.layout.UserLayout;
import ru.incoremedia.gwttest.application.client.view.user.notesgroup.NotesGroup;
import ru.incoremedia.gwttest.core.client.component.autosizedtextarea.AutoSizedTextArea;
import ru.incoremedia.gwttest.core.client.component.autosizedtextarea.TextChangeHandler;


public class NoteItem extends Composite {

    public interface Styles extends CssResource {
        String newNoteItem();
        String moveCursor();
        String defaultCursor();
        String target();
        String completeNoteItem();
        String deleteNoteItem();
        String completeButtonLight();
        String deleteButtonLight();
        String deleteControlZone();
        String completeControlZone();
    }

    @UiField Styles style;

    private boolean isNew = false;

    private boolean isComplete = false;

    private boolean isDelete = false;

    interface MyUiBinder extends UiBinder<Widget, NoteItem> {}

    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    @UiField DragZone dragZone;

    @UiField SimplePanel controlZone;
    
    @UiField AutoSizedTextArea textArea;

    @UiField Button completeButton;

    @UiField Button deleteButton;
    
    private NotesGroup notesGroup;

    public NoteItem(final NotesGroup notesGroup) {
        this.notesGroup = notesGroup;

        initWidget(uiBinder.createAndBindUi(this));

        textArea.addTextChangeHandler(new TextChangeHandler() {
            @Override
            public void onChange() {
                dragZone.setHeight(textArea.getHiddenDiv().getElement().getClientHeight() + "px");
                controlZone.setHeight(textArea.getHiddenDiv().getElement().getClientHeight() + "px");
            }
        });

        textArea.addFocusHandler(new FocusHandler() {
            @Override
            public void onFocus(FocusEvent event) {
                if(isNew()) {
                    textArea.setText("");
                }
            }
        });

        textArea.addBlurHandler(new BlurHandler() {
            @Override
            public void onBlur(BlurEvent event) {
                if(isNew()) {
                    setNew(true);
                }
            }
        });

        completeButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {                
                if(!isComplete) {
                    setComplete(true);
                } else {
                    setComplete(false);
                }
            }
        });

        deleteButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if(!isDelete) {
                    setDelete(true);
                } else {
                    setDelete(false);
                }
            }
        });

        textArea.setHiddenDiv(UserLayout.getInstance().getUserContent().getHiddenDiv());
        setNew(true);
        textArea.correctLines();
    }

    public void setComplete(boolean isComplete) {
        if(!isNew) {
            this.isComplete = isComplete;
            if(isComplete) {
                if(!notesGroup.isDelete()) {
                    setDelete(false);
                    textArea.addStyleName(style.completeNoteItem());
                    completeButton.addStyleName(style.completeButtonLight());
                    controlZone.addStyleName(style.completeControlZone());
                    textArea.setReadOnly(true);
                }
            } else {                
                textArea.removeStyleName(style.completeNoteItem());
                completeButton.removeStyleName(style.completeButtonLight());
                controlZone.removeStyleName(style.completeControlZone());
                textArea.setReadOnly(false);
                isComplete = false;
            }
        }
    }

    public void setDelete(boolean isDelete) {
        if(!isNew) {
            this.isDelete = isDelete;
            if(isDelete) {
                setComplete(false);
                textArea.addStyleName(style.deleteNoteItem());
                deleteButton.addStyleName(style.deleteButtonLight());
                controlZone.addStyleName(style.deleteControlZone());
                textArea.setReadOnly(true);
            } else {
                if(!notesGroup.isDelete()) {
                    textArea.removeStyleName(style.deleteNoteItem());
                    deleteButton.removeStyleName(style.deleteButtonLight());
                    controlZone.removeStyleName(style.deleteControlZone());
                    textArea.setReadOnly(false);
                    isDelete = false;
                }
            }
        }
    }

    public void setNew(boolean isNew) {
        this.isNew = isNew;
        if(isNew) {
            textArea.setText(I18N.constants.newNoteText());
            textArea.addStyleName(style.newNoteItem());
            controlZone.addStyleName(style.newNoteItem());
        } else {
            textArea.removeStyleName(style.newNoteItem());
            controlZone.removeStyleName(style.newNoteItem());
        }
    }

    public boolean isNew() {
        return isNew;
    }

    public AutoSizedTextArea getTextArea() {
        return textArea;
    }

    public DragZone getDragZone() {
        return dragZone;
    }

    public NotesGroup getNotesGroup() {
        return notesGroup;
    }

    public void setNotesGroup(NotesGroup notesGroup) {
        this.notesGroup = notesGroup;
    }

    public void disableTargetStyle() {
        textArea.removeStyleName(style.target());
        controlZone.removeStyleName(style.target());
    }

    public void enableTargetStyle() {
        textArea.addStyleName(style.target());
        controlZone.addStyleName(style.target());
    }

    public void enableMoveStyle() {
        textArea.addStyleName(style.defaultCursor());
        dragZone.removeStyleName(style.moveCursor());
        textArea.setFocus(false);
    }

    public void disableMoveStyle() {
        textArea.removeStyleName(style.defaultCursor());
        dragZone.addStyleName(style.moveCursor());
    }

    public boolean isComplete() {
        return isComplete;
    }

    public boolean isDelete() {
        return isDelete;
    }
}
